layui.use(['layer', 'form','table'], function() {
    const layer = layui.layer;
    const $ = layui.$;
    const table = layui.table;
    const form = layui.form;


    let userTab = table.render({
        elem: '#userTab',
        url: '/activiti/user/list', //数据接口
        page: true, //开启分页
        request: {
            pageName: 'pageNum', // page
            limitName: 'pageSize' // limit
        },
        cols: [[ //表头
            {field: 'id', title: '用户ID', width:150, sort: true, fixed: 'left'},
            {field: 'firstName', title: '用户名'},
            {field: 'lastName', title: '中文名'},
            {field: 'operate', title: '操作',toolbar: '#operate'}
        ]],
        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.code, //返回状态码
                "msg": res.msg, //解析提示信息
                "count": res.data.total, //后台返回的数据总条数，用于自定义分页使用
                "data": res.data.list //解析数据列表
            };
        }
    });


    form.on('submit(query)',function(data){
        userTab.reload({
            where: data.field
            ,page: {
                curr: 1
            }
        });
        return false;
    });


    form.on('submit(add)',function(){
        let html = document.getElementById('addUser').innerHTML;
        layer.open({
            title: '新增用户',
            type: 1,
            content: html,
            area: ['650px', '400px'],
            btn: ['确定', '取消'],
            success: function(){
                // 初始化用户下拉框
                $.ajax({
                    url:"/user/query",
                    type:"get",
                    success: function(res){
                        if (res.code==0){
                            let userArr = res.data;
                            $.each(userArr,function(index, user){
                                $('#id').append(new Option(user.adminName, user.adminID))
                            });
                            form.render('select')
                        }else{
                            layer.msg("加载用户信息报错",{icon:5})
                        }
                    }
                })

            },
            yes: function(){
                $('#save').click();
            },
            btn2: function(){
                layer.closeAll();
            }
        });
        return false;
    });


    form.on('submit(save)',function(data){
        $.ajax({
            url:"/activiti/user/save",
            dataType:"json",
            data:data.field,
            type:"post",
            success:function(res){
                if (res.code==0){
                    userTab.reload()
                    layer.closeAll()
                    layer.msg(res.msg,{icon:6})
                }else{
                    layer.msg(res.msg,{icon:5})
                }
            },
            error:function(){
                console.log("error")
                layer.msg("系统错误",{icon:5})
            }
        });
    });


    table.on('tool(userTab)', function (obj){
        if (obj.event == 'edit'){
            let html = document.getElementById('addUser').innerHTML;
            layer.open({
                title: '修改用户',
                type: 1,
                content: html,
                area: ['650px', '400px'],
                btn: ['确定', '取消'],
                success:function(){
                    form.val('addUser',obj.data)
                },
                yes: function(){
                    $('#save').click();
                },
                btn2: function(){
                    layer.closeAll();
                }
            });
        } else if (obj.event == 'del'){
            $.ajax({
                url:"/activiti/user/delete?id="+obj.data.id,
                type:"delete",
                success:function(res){
                    if (res.code==0){
                        layer.msg(res.msg,{icon:6})
                        userTab.reload()
                    }else{
                        layer.msg(res.msg,{icon:5})
                    }
                },
                error:function(){
                    layer.msg("系统错误",{icon:5})
                }
            })
        }
    })
});